home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / gold / dodads.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.4 KB  |  319 lines

  1. /*
  2.  * The original copyright owners of the accompanying source code files have
  3.  * agreed to place such code into the public domain.  Accordingly, anyone
  4.  * who receives or obtains a copy of such source code is freely entitled to
  5.  * reproduce, use and otherwise exploit such code (including the right to
  6.  * make derivative works), at his/her own risk and expense, without any
  7.  * obligation or liability to the original copyright owners.
  8.  *
  9.  * We would appreciate (but do not require) that the following message be
  10.  * included in any derivative works:
  11.  *
  12.  * "Portions of this program were developed by Peter Broadwell, Rob Myers
  13.  * and Robin Schaufler while working in Silicon Valley."
  14.  *
  15.  * The accompanying source code files and related documentation materials
  16.  * are distributed on an "AS IS" basis, without any warranties or
  17.  * guarantees of any kind.  All implied warranties, including the implied
  18.  * warranties of merchantability and of fitness for any particular purpose,
  19.  * are expressly disclaimed.
  20.  */
  21. #include "gl.h"
  22. #include "colors.h"
  23. #include "sounds.h"
  24.  
  25. #include <sys/types.h>
  26. #include <sys/times.h>
  27. #include "timeb.h"
  28.  
  29. struct timeb tb_start, tb_end;        /* timer blocks        */
  30. extern int maldbug;
  31.  
  32. char * membuf[256];
  33. long memuse[256];
  34. long memufre[256];
  35. extern long fishPop;
  36. extern long numgups;
  37.  
  38. #ifdef MALDEBUG
  39. #define Bzero(a,b)    { char *q=(a), *r=(b); \
  40.             if(maldbug) printf("bzeroing(0x%x,0x%x)\n",q,r); \
  41.             bzero(q,r);}
  42. #else
  43. #define Bzero(a,b)    bzero(a,b)
  44. #endif /* MALDEBUG */
  45.  
  46. #undef BUSY
  47. #define BUSY 1
  48. #define INT int
  49. #define testbusy(p) ((INT)(p)&BUSY)
  50. #define clearbusy(p) ((INT)(p)&~BUSY)
  51.  
  52. union store {
  53.     union store *ptr;
  54.     int dummy[1];
  55.     int calloc;        /*calloc clears an array of integers*/
  56. };
  57.  
  58. gffree(ap)
  59.     register char * ap;
  60. {
  61. #ifdef FUDGE
  62. #define FUDGE
  63.     register union store *p = (union store *)ap;
  64.     register long sz;
  65.  
  66.         /* mallocs info is just before cell */
  67.     --p;
  68.     sz = (char *)clearbusy(p->ptr) - ap;
  69.  
  70. Bzero(ap,sz);
  71.     if(sz<256) {
  72.     *((char **)ap) = membuf[sz];
  73.     membuf[sz] = (char *)ap;
  74.     if(memufre[sz] == 0) {
  75.         if(maldbug)
  76.         printf("gffree'ing %d bytes\n",sz);
  77.     }
  78.     memufre[sz] += 1;
  79.     return;
  80.     }
  81. #endif /* FUDGE */
  82.     free(ap);
  83. }
  84.  
  85.     char *
  86. gfmalloc(sz)
  87.     long sz;
  88. {
  89.     register char * retval;
  90.  
  91.     if(sz<256) {
  92.     if(memuse[sz] == 0) {
  93.         if(maldbug)
  94.         printf("gfmalloc'ing %d bytes\n",sz);
  95.     }
  96.     memuse[sz] += 1;
  97.     if(membuf[sz] != 0) {
  98.         retval = membuf[sz];
  99.         membuf[sz] = *(char **)retval;
  100.         return retval;
  101.     }
  102.     }
  103.     return (char *)malloc(sz);
  104. }
  105.  
  106. /*
  107.  Store the start time for each kind of sounds and don't bother to
  108.  start another copy of it if it couldn't have expired yet...
  109. */
  110.  
  111. typedef struct sndBndl {
  112.     long nexttime;
  113.     long duration;
  114. } sndBndl;
  115.  
  116. sndBndl snds[MAX_SOUND] = {
  117. {0, 0 /* dummy pad slot */ }, 
  118. {0, BUG_EYE_LENGTH }, 
  119. {0, LAY_EGGS_LENGTH }, 
  120. {0, MUNCH_LENGTH }, 
  121. };
  122.  
  123. void
  124. playSong(char *sname)
  125. {
  126.     char buf[200];
  127.     
  128.     sprintf(buf, "playaiff %s/%s&", SOUND_DIR, sname);
  129.     system(buf);
  130. }
  131.  
  132. void
  133. doSound(int snd)
  134. {
  135.     struct tms tmsbuf;
  136.     long now;
  137.     char buf[200];
  138.  
  139.     switch(snd) {
  140.     case BUG_EYE:
  141.     case MUNCH:
  142.     case LAY_EGGS:
  143.         now = times(&tmsbuf);
  144.         if(snds[snd].nexttime < now) {
  145.         switch(snd) {
  146.             case BUG_EYE: playSong("heartBeat.aiff");
  147.             break;
  148.             case MUNCH: playSong("munch1.aiff");
  149.             break;
  150.             case LAY_EGGS: playSong("newEggs.aiff");
  151.             break;
  152.         }
  153.         snds[snd].nexttime = snds[snd].duration + now;
  154.         }
  155.     break;
  156.     default: printf("doSound: got bad arg >%d<\n", snd);
  157.     break;
  158.     }    
  159. }
  160.  
  161. drawOrig()
  162. {
  163.     pushmatrix();
  164.     scale(100.0,100.0,100.0);
  165.     fishColor(BOWL_COLOR);
  166.     movei(-40,0,0);
  167.     drawi(40,0,0);
  168.       /* drawi(40,20,0); /* */
  169.     cmovi(40,0,0);
  170.     charstr("x");
  171.     movei(0,-40,0);
  172.     drawi(0,40,0);
  173.       /* drawi(20,40,0); /* */
  174.     cmovi(0,40,0);
  175.     charstr("y");
  176.     movei(0,0,-40);
  177.     drawi(0,0,40);
  178.       /* drawi(20,0,40); /* */
  179.     cmovi(0,0,40);
  180.     charstr("z");
  181.     popmatrix();
  182. }
  183.  
  184. drawBowl()
  185. {
  186.     pushmatrix();
  187.     scale(100.0,100.0,100.0);
  188.     fishColor(BOWL_COLOR);
  189.  
  190.     translate(0.0,0.0,-50.0);
  191.     circi(0,0,50);
  192.     translate(0.0,0.0,100.0);
  193.     /* 
  194.     translate(0.0,0.0,50.0);
  195.     */
  196.     circi(0,0,50);
  197.  
  198.     popmatrix();
  199. }
  200.  
  201. drawBox()
  202. {
  203.     pushmatrix();
  204.     scale(100.0,100.0,100.0);
  205.     fishColor(BOWL_COLOR);
  206.  
  207.     movei(-40,-40,-40);
  208.     drawi(-40, 40,-40);
  209.     drawi(-40, 40, 40);
  210.     drawi(-40,-40, 40);
  211.     drawi(-40,-40,-40);
  212.  
  213.     movei( 40,-40,-40);
  214.     drawi( 40, 40,-40);
  215.     drawi( 40, 40, 40);
  216.     drawi( 40,-40, 40);
  217.     drawi( 40,-40,-40);
  218.  
  219.     movei(-40, 40,-40);
  220.     drawi( 40, 40,-40);
  221.     drawi( 40, 40, 40);
  222.     drawi(-40, 40, 40);
  223.     drawi(-40, 40,-40);
  224.  
  225.     movei(-40,-40,-40);
  226.     drawi( 40,-40,-40);
  227.     drawi( 40,-40, 40);
  228.     drawi(-40,-40, 40);
  229.     drawi(-40,-40,-40);
  230.  
  231.     popmatrix();
  232. }
  233.  
  234. #define dy 14
  235. #define y 300
  236. display_time (name, iteration, count)
  237.     char *name;
  238.     register long iteration, count;
  239. {
  240.     register int millisecs;
  241.     char charbuf[100];
  242.  
  243.     millisecs = 1000 * (tb_end.time - tb_start.time) +
  244.             tb_end.millitm - tb_start.millitm;
  245.     if (millisecs == 0) millisecs = 1;
  246.  
  247.     fishWritemask(FOREGROUND);
  248.     pushmatrix();
  249.     ortho2(0.0,500.0,0.0,500.0);
  250.     cursoff ();
  251.     frontbuffer (TRUE);
  252.     color (WHITE);
  253.  
  254.     sprintf (charbuf,"Times for %s: iteration %d",name, iteration);
  255.     cmov2i (150,y);
  256.     charstr (charbuf);
  257.  
  258.     sprintf (charbuf,"    %d in %d.%02d seconds",
  259.         count,millisecs/1000,(millisecs%1000)/10);
  260.     cmov2i (150,y-dy);
  261.     charstr (charbuf);
  262.  
  263.     sprintf (charbuf,"    %d msecs per call",millisecs/count);
  264.     cmov2i (150,y-2*dy);
  265.     charstr (charbuf);
  266.  
  267.     sprintf (charbuf,"    %d per second",count*1000/millisecs);
  268.     cmov2i (150,y-3*dy);
  269.     charstr (charbuf);
  270.  
  271.     sprintf (charbuf,"    %d total fish",fishPop);
  272.     cmov2i (150,y-4*dy);
  273.     charstr (charbuf);
  274.  
  275.     sprintf (charbuf,"    %d total gups",numgups);
  276.     cmov2i (150,y-5*dy);
  277.     charstr (charbuf);
  278.  
  279.     frontbuffer (FALSE);
  280.     curson ();
  281.     popmatrix();
  282. }
  283.  
  284. ftime (tb)
  285.     register struct timeb *tb;
  286. {
  287.     long s60;
  288.     struct tms tmsbuf;
  289.  
  290.     s60 = times(&tmsbuf);
  291.     tb -> time = s60/60;
  292.     tb -> millitm = (s60%60) * 1000 / 60;
  293.     return s60;
  294. }
  295.  
  296. #ifdef NOTDEF
  297. #include <gl2/gltypes.h>
  298. #include <gl2/globals.h>
  299. #endif
  300.  
  301. dumpglobjs()
  302. {
  303. #ifdef NOTDEF
  304.     register long i;
  305.     register hashrec *h;
  306.  
  307.     printf("dumping:\n");
  308.     for(i=0;i<256;i++) {
  309.     h = gl_hashtable[i];
  310.     if(h) printf("\n");
  311.     for(; h; h = h->link) {
  312.         printf(" 0x%x",h->obj);
  313.     }
  314.     }
  315.     printf("\n");
  316. #endif
  317. }
  318.  
  319.